﻿using System;
using System.Web;
using Token.Piker.Core.Security;
using Token.Piker.Common;


namespace Token.Piker.Core
{
    /// <summary>
    ///PikerBasePage 的摘要说明
    /// </summary>
    public class PikerBasePage : System.Web.UI.Page
    {
        private bool isLogin = false;
        public bool IsLogin
        {
            get { return isLogin; }
        }


        //用户名
        private string userName = null;
        public string UserName
        {
            get { return userName; }

        }


        protected override void OnLoad(EventArgs e)
        {
            //HttpCookie cookie = Request.Cookies[GlobalConstants.CK];
            ////如果有cookie
            //if (cookie != null)//注册脚本
            //{
            //    userName = EncryptionProviderFactory.GetEncryptor().Decode(cookie.Value);
            //    isLogin = true;
            //    base.OnLoad(e);
            //    OutputScripts();
            //    return;

            //}
            //如果存在session
            if (Session[GlobalConstants.PIKER_USER_NAME] != null)
            {
                userName = Session[GlobalConstants.PIKER_USER_NAME].ToString();
                isLogin = true;
                base.OnLoad(e);
                OutputScripts();
                return;
            }

            ////设置cookie 此时传了用户名和设置过期时间
            //if (!String.IsNullOrEmpty(Request.QueryString[GlobalConstants.CHECKED]))
            //{
            //    string encodeName = Request.QueryString[GlobalConstants.TOKEN].ToString();
            //    HttpCookie setcookie = new HttpCookie(GlobalConstants.CK, encodeName);
            //    setcookie.Expires = DateTime.Today.AddMonths(1);
            //    Response.AppendCookie(setcookie);
            //    userName = EncryptionProviderFactory.GetEncryptor().Decode(encodeName);
            //    isLogin = true;
            //}//设置session
            //else if (!String.IsNullOrEmpty(Request.QueryString[GlobalConstants.TOKEN]))
            //{
            //    string encodeName = Request.QueryString[GlobalConstants.TOKEN].ToString();
            //    userName = EncryptionProviderFactory.GetEncryptor().Decode(encodeName);
            //    isLogin = true;
            //    Session[GlobalConstants.PIKER_USER] = userName;

            //}
            base.OnLoad(e);
            OutputScripts();
        }

        private void OutputScripts()
        {

            if (!string.IsNullOrEmpty(userName))
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "body", "<script type=\"text/javascript\" >    Page={IsLogin:'" + isLogin.ToString().ToUpper() + "', UserName:'" + userName + "',HostUrl:'" + Server.HtmlEncode(Request.Url.AbsoluteUri) + "'};</script>");
            }
            this.ClientScript.RegisterStartupScript(this.GetType(), "body", "<script type=\"text/javascript\" >    Page={IsLogin:'" + isLogin.ToString().ToUpper() + "',HostUrl:'" + Server.HtmlEncode(Request.Url.AbsoluteUri) + "'}; </script>");
        }



        //private string prefix = "http://www.wutnews.net/my/login.aspx?from=";
        //public string LoginPage
        //{
        //    get
        //    {
        //        return prefix + Server.HtmlEncode(Request.Url.AbsoluteUri);
        //    }
        //}


        ////使用此方法验证
        //protected void CheckUser()
        //{
        //    if (!isLogin)//未登录 跳转
        //    {
        //        Response.Redirect(LoginPage);
        //    }
        //    else
        //    {

        //    }

        //}








    }
}

